#include<bits/stdc++.h>
using namespace std;
#define int long long
int q,l,r,a[100],b[100],f[40][2][2];
int F(int i,bool up,bool upl,int nu){
	if(i<0) return 1;
	if(~f[i][up][upl]) return f[i][up][upl];
	int ans=0;
	for(int v=0;v<=1;++v)
	for(int v1=0;v1<=v;++v1){
		if(up&&v>a[i]) continue;
		if(upl&&v1<b[i]) continue;
		ans+=F(i-1,up&&v==a[i],upl&&v1==b[i],nu|(v<<i));
	}
	return f[i][up][upl]=ans;
}
signed main(){
	freopen("comb.in","r",stdin);
	freopen("comb.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>q;
	while(q--){
		cin>>l>>r;
		memset(f,-1,sizeof(f));
		for(int i=0;i<=30;++i) a[i]=(r>>i)&1,b[i]=(l>>i)&1;
		int ans=F(30,1,1,0);
		cout<<ans<<'\n'; 
	}
	return 0;
}


